package cn.edu.seu.herald.informationplatform.dao.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.edu.seu.herald.informationplatform.bean.league.Recruit;
import cn.edu.seu.herald.informationplatform.bean.league.RecruitJoin;

import com.mysql.jdbc.Statement;

public class RecruitJoinDao extends DataAccessObject{
	private static RecruitJoinDao daoInstance=null;
	protected RecruitJoinDao(){
	}
	public static RecruitJoinDao getDao(){
		if(daoInstance==null){
			daoInstance=new RecruitJoinDao();
		}
		return daoInstance;
	}
	
	//add
	public int addRecruitJoin(RecruitJoin recruitJoin) throws SQLException{
		Connection con=getConnectionLeague();
		try {
			PreparedStatement ps=con.prepareStatement("insert into lg_recruit_join(recuit_id,time,resume_address,true_name,grade,college,contact,details) values (?,?,?,?,?,?,?,?)",Statement.RETURN_GENERATED_KEYS);
			ps.setInt(1,recruitJoin.getRecuitId());
			ps.setDate(2,recruitJoin.getTime());
			ps.setString(3,recruitJoin.getResumeAddress());
			ps.setString(4,recruitJoin.getTureName());
			ps.setString(5,recruitJoin.getGrade());
			ps.setString( 6, recruitJoin.getCollege());
			ps.setString(7,recruitJoin.getContact());
			ps.setString(8,recruitJoin.getDetails());
			ps.executeUpdate();
			ResultSet rsResultSet=ps.getGeneratedKeys();
			int new_id=-1;
			if(rsResultSet.next()){
				new_id=rsResultSet.getInt(1);
			}
			 closeRs(rsResultSet);
			return new_id;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		finally{
			closeCon(con);
		}
	}
	
	//delete
	public void deleteRecruitJoin(int id) throws SQLException{
		Connection con=getConnectionLeague();
		try {
			PreparedStatement ps=con.prepareStatement("delete from lg_recruit_join where id=?");
			ps.setInt(1,id);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		finally{
			closeCon(con);
		}
	}
	
	//update
	
	
	//select by recruidId return object of Recruit
	public Recruit getRecruitJoinByRecruitId(int recruidId ) throws SQLException{
		Connection con=getConnectionLeague();
    	try{
    	PreparedStatement ps=con.prepareStatement("select * from lg_recruit where league_id=? ");
    	ps.setInt(1,recruidId);
    	ResultSet rsResultSet=ps.executeQuery();
    	Recruit recruit=new Recruit();
    	while(rsResultSet.next()){
    		recruit.setInformation(rsResultSet.getString(3));
    		recruit.setContact(rsResultSet.getString(4));
    		recruit.setReleaseTime(rsResultSet.getDate(5));
    		recruit.setAttachmentAddress(rsResultSet.getString(6));
    	}
    	closeRs(rsResultSet);
    	return recruit;
    	}catch (SQLException e){
		e.printStackTrace();
		throw e;
	}
	finally{
		closeCon(con);
	}
	}
}
